from utils import *

# Updates opponent's preflop actions and the (# raises, # calls, # folds)
def addPreflopActions(opp, preflopActions):
    actions = []
    for action in preflopActions:
        if not action.isType('POST'):
            actions.append(action)
    if len(actions) > 1 and actions[-1].type == Action.CALL:
        actions = actions[:-1]
    opp.preflopActions.append(actions)

# Updates opponent's preflop strengths
def addPreflopStrengths(opp, strength):
    opp.preflopStrengths.append([strength])

# Adjust opponent's preflop tightness and aggression
def adjPreflopBehavior(opp):
    raiseTotal, callTotal, total = 0, 0, 0
    for i in range(len(opp.preflopActions)):
        for action in opp.preflopActions[i]:
            weight = max(1 - 0.01 * (len(opp.preflopActions) - i - 1), 0.01)
            if action.isType('RAISE'):
                raiseTotal += weight
            elif action.isType('CALL'):
                callTotal += weight
            total += weight
    total = max(total, 1.)
    raiseRatio, callRatio = raiseTotal / total, callTotal / total
    a, b = max(100 - len(opp.preflopActions), 0) / 100., min(len(opp.preflopActions), 100) / 100.
    opp.preflopAggro = OPP_PREFLOP_AGGRO * a + raiseRatio * b
    opp.preflopTight = OPP_PREFLOP_TIGHT * a + (raiseRatio + callRatio) * b

# Updates opponent's postflop actions and the (# raises, # calls, # folds)
def addPostflopActions(opp, postflopActions):
    actions = []
    for action in postflopActions:
        if not action.isType('SHOW'):
            actions.append(action)
    if len(actions) > 1 and actions[-1].type == Action.CALL:
        actions = actions[:-1]
    opp.postflopActions.append(actions)

# Updates opponent's postflop strengths
def addPostflopStrengths(opp, strength):
    opp.postflopStrengths.append([strength])

# Adjust opponent's postflop tightness and aggression
def adjPostflopBehavior(opp):
    raiseTotal, callTotal, total = 0, 0, 0
    for i in range(len(opp.postflopActions)):
        for action in opp.postflopActions[i]:
            weight = max(1 - 0.005 * (len(opp.postflopActions) - i - 1), 0.01)
            if action.isType('RAISE'):
                raiseTotal += weight
            elif action.isType('CALL'):
                callTotal += weight
            total += weight
    total = max(total, 1.)
    raiseRatio, callRatio = raiseTotal / total, callTotal / total
    a, b = max(300 - len(opp.postflopActions), 0) / 300., min(len(opp.postflopActions), 300) / 300.
    opp.postflopAggro = OPP_POSTFLOP_AGGRO * a + raiseRatio * b
    opp.postflopTight = OPP_POSTFLOP_TIGHT * a + (raiseRatio + callRatio) * b
    if opp.postflopAggro < .05:
        opp.postflopAggro = .05
    if opp.postflopTight < .1:
        opp.postflopTight = .1
